import os
import shutil

# 远程扫描目录路径
import sys
from datetime import datetime

from logger import Logger

remote_path_list = [
    r'\\diskstation3\10T_01\Fitgirl',
    r'\\diskstation3\10T_02\Fitgirl',
    r'\\diskstation3\14T_02\8192G_04\Fitgirl',
]

# 目标目录路径
target_path = r"\\diskstation3\14T_02\8192G_04\Fitgirl20250122"
# result_path = r"\\diskstation3\8192G_04\Fitgirl20250111_result"

# 获取远程目录下的文件列表
existing_file_dict = {}
sys.stdout = Logger(datetime.now().strftime("log/log_%Y%m%d_%H%M%S") + ".txt")

for remote_path in remote_path_list:
    file_list = os.listdir(remote_path)
    for file_name in file_list:
        print(f"scanning {file_name}")
        # 构建文件的绝对路径
        file_path = os.path.join(remote_path, file_name)
        if file_name in existing_file_dict:
            print(f'warn: {file_name} already exists!')
        existing_file_dict[file_name] = remote_path
        # directory, filename = os.path.split(file_path)
        # existing_file_dict[filename] = directory


for file_name in os.listdir(target_path):
    if file_name in existing_file_dict:
        # 有重复的
        target_temp_dir = os.path.join(target_path + "_result", existing_file_dict[file_name].replace("\\", "_").replace("/", "_"))
        if not os.path.isdir(target_temp_dir) and os.path.dirname(target_temp_dir) != "":
            os.makedirs(target_temp_dir)
        shutil.move(os.path.join(target_path, file_name), os.path.join(target_temp_dir, file_name))
        print(f'dup found: {file_name} in {existing_file_dict[file_name]}')
        # 要把重复的原文件也移动出去
        # parent_dir = os.path.dirname(existing_file_dict[file_name])
        source_temp_dir = existing_file_dict[file_name] + "_result"
        if not os.path.isdir(source_temp_dir) and os.path.dirname(source_temp_dir) != "":
            os.makedirs(source_temp_dir)
        shutil.move(os.path.join(existing_file_dict[file_name], file_name), os.path.join(source_temp_dir, file_name))
        pass
print("done")
